﻿using MODEL;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL
{
   public class GoodDAL
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<GoodsModel> SelectGoods(int id)
        {
            string sql = @"select * from Goods WHERE FK_CarriersID=@FK_CarriersID";
            //定义查询列表和参数列表
            SqlParameter[] parm = {
                new SqlParameter("@FK_CarriersID",id)
            };
            SqlDataReader reader = DBhelper.ExecuteReader(sql, parm);
            List<GoodsModel> list = new List<GoodsModel>();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    GoodsModel good = new GoodsModel();
                    good.GoodsID = Convert.ToInt32(reader["GoodsID"]);
                    good.GoodsName = reader["GoodsName"] as string;
                    good.Amount = Convert.ToInt32(reader["Amount"]);
                    good.Weight = float.Parse(Convert.ToString(reader["Weight"]));
                    good.Volume = float.Parse(Convert.ToString(reader["Volume"]));
                    good.FK_CarriersID = Convert.ToInt32(reader["FK_CarriersID"]);
                    list.Add(good);
                }
            }
            return list;
        }

        //添加
        public int InsertGoods(GoodsModel inse)
        {
            string insert = @"insert into Goods(GoodsName,Amount,Weight,Volume,FK_CarriersID,IsDelete) values(@GoodsName,@Amount,@Weight,@Volume,@FK_CarriersID,@IsDelete)";
            SqlParameter[] pram = {
                new SqlParameter("@GoodsName",inse.GoodsName),
                new SqlParameter("@Amount",inse.Amount),
                new SqlParameter("@Weight",inse.Weight),
                new SqlParameter("@Volume",inse.Volume),
                new SqlParameter("@FK_CarriersID",inse.FK_CarriersID),
                new SqlParameter("@IsDelete",false),
            };
            return DBhelper.ExecuteNonQuery(insert, pram);
        }
    }
}
